package com.hsw.bledemo.review.algorithm

/**
 * @author: HSW
 * @data: 2024/8/27
 * @desc:
 */
fun selectionSort(array: Array<Int>) {
    val length = array.size

    for (i in 0 until (length - 1)) {

        var minIndex = i

        for (j in i until length) {

            if (array[j] < array[minIndex]) {
                minIndex = j
            }
        }
        val temp = array[minIndex]
        array[minIndex] = array[i]
        array[i] = temp
    }
    println(array.joinToString(separator = ","))
}

fun bubbleSort(array: Array<Int>) {

    val length = array.size

    for (i in 0 until length) {
        for (j in 0 until length - i - 1) {
            if (array[j] > array[j + 1]) {
                val temp = array[j + 1]
                array[j + 1] = array[j]
                array[j] = temp;
            }
        }
    }
    println(array.joinToString(separator = ","))
}

fun insertSort(array: Array<Int>) {
    val length = array.size

    for (i in 1 until length) {
        var preIndex = i - 1
        val current = array[i]

        while (preIndex >= 0 && current < array[preIndex]) {
            array[preIndex + 1] = array[preIndex]
            preIndex--;
        }

        array[preIndex + 1] = current
    }
    println(array.joinToString(separator = ","))
}
